home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / unexhp9k800.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-25  |  10.8 KB  |  362 lines

  1. /* Unexec for HP 9000 Series 800 machines.
  2.    Bob Desinger <hpsemc!bd@hplabs.hp.com>
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: Not synched with FSF. */
  21.  
  22. /*
  23.  
  24.   Unexec creates a copy of the old a.out file, and replaces the old data
  25.   area with the current data area.  When the new file is executed, the
  26.   process will see the same data structures and data values that the
  27.   original process had when unexec was called.
  28.   
  29.   Unlike other versions of unexec, this one copies symbol table and
  30.   debug information to the new a.out file.  Thus, the new a.out file
  31.   may be debugged with symbolic debuggers.
  32.   
  33.   If you fix any bugs in this, I'd like to incorporate your fixes.
  34.   Send them to uunet!hpda!hpsemc!jmorris or jmorris%hpsemc@hplabs.HP.COM.
  35.   
  36.   CAVEATS:
  37.   This routine saves the current value of all static and external
  38.   variables.  This means that any data structure that needs to be
  39.   initialized must be explicitly reset.  Variables will not have their
  40.   expected default values.
  41.   
  42.   Unfortunately, the HP-UX signal handler has internal initialization
  43.   flags which are not explicitly reset.  Thus, for signals to work in
  44.   conjunction with this routine, the following code must executed when
  45.   the new process starts up.
  46.   
  47.   void _sigreturn();
  48.   ...
  49.   sigsetreturn(_sigreturn);
  50. */
  51.  
  52.  
  53. #include <config.h>
  54. #include <stdio.h>
  55. #include <fcntl.h>
  56. #include <errno.h>
  57.  
  58. #include <a.out.h>
  59.  
  60. /*
  61.  * Minor modification to enable dumping with shared libraries added by
  62.  * Dipankar Gupta (dg@hplb.hpl.hp.com). I studied Oliver Laumann's
  63.  * more elaborate dynamic loading scheme in ELK while implementing
  64.  * this, but don't use any of his machinery.
  65.  *
  66.  * Stores the BRK value at dump time, and uses the RUN_TIME_REMAP hook
  67.  * to break back to the stored value when the dumped executable is restarted.
  68.  *
  69.  * CAVEATS (addenda):
  70.  * 1. Text area of the shlibs are not stored. Thus, if a shared library is
  71.  *    replaced between the time of dump and execution, all bets are off.
  72.  *
  73.  * 2. Assumes that the data and bss area are adjacent, which is true of the
  74.  *    current VM implementation.
  75.  *
  76.  * 3. Any setup that defines HPUX_USE_SHLIBS *must* also define
  77.  *    RUN_TIME_REMAP.  
  78.  */
  79.  
  80. #ifdef HPUX_USE_SHLIBS
  81. #include <dl.h>            /* User-space dynamic loader entry points */
  82. void Save_Shared_Data();
  83. int run_time_remap();
  84. #endif
  85.  
  86. #define roundup(x,n) ( ( (x)+(n-1) ) & ~(n-1) )  /* n is power of 2 */
  87. #define min(x,y)  ( ((x)<(y))?(x):(y) )
  88.  
  89.  
  90. /* Create a new a.out file, same as old but with current data space */
  91.  
  92. unexec(new_name, old_name, new_end_of_text, dummy1, dummy2)
  93.      char new_name[];        /* name of the new a.out file to be created */
  94.      char old_name[];        /* name of the old a.out file */
  95.      char *new_end_of_text;    /* ptr to new edata/etext; NOT USED YET */
  96.      int dummy1, dummy2;    /* not used by emacs */
  97. {
  98.   int old, new;
  99.   int old_size, new_size;
  100.   struct header hdr;
  101.   struct som_exec_auxhdr auxhdr;
  102.   long i;
  103.   
  104.   /* For the greatest flexibility, should create a temporary file in
  105.      the same directory as the new file.  When everything is complete,
  106.      rename the temp file to the new name.
  107.      This way, a program could update its own a.out file even while
  108.      it is still executing.  If problems occur, everything is still
  109.      intact.  NOT implemented.  */
  110.   
  111.   /* Open the input and output a.out files */
  112.   old = open (old_name, O_RDONLY);
  113.   if (old < 0)
  114.     { perror(old_name); exit(1); }
  115.   new = open (new_name, O_CREAT|O_RDWR|O_TRUNC, 0777);
  116.   if (new < 0)
  117.     { perror(new_name); exit(1); }
  118.   
  119.   /* Read the old headers */
  120.   read_header(old, &hdr, &auxhdr);
  121.   
  122. #ifdef HPUX_USE_SHLIBS
  123.   Save_Shared_Data(); /* Save break value (added: dg@hplb.hpl.hp.com) */
  124. #endif
  125.   /* Decide how large the new and old data areas are */
  126.   old_size = auxhdr.exec_dsize;
  127.   /* I suspect these two statements are separate
  128.      to avoid a compiler bug in hpux version 8.  */
  129.   i = (long) sbrk (0);
  130.   new_size = i - auxhdr.exec_dmem;
  131.   
  132.   /* Copy the old file to the new, up to the data space */
  133.   lseek(old, 0, 0);
  134.   copy_file(old, new, auxhdr.exec_dfile);
  135.   
  136.   /* Skip the old data segment and write a new one */
  137.   lseek(old, old_size, 1);
  138.   save_data_space(new, &hdr, &auxhdr, new_size);
  139.   
  140.   /* Copy the rest of the file */
  141.   copy_rest(old, new);
  142.   
  143.   /* Update file pointers since we probably changed size of data area */
  144.   update_file_ptrs(new, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size);
  145.   
  146.   /* Save the modified header */
  147.   write_header(new, &hdr, &auxhdr);
  148.   
  149.   /* Close the binary file */
  150.   close (old);
  151.   close (new);
  152.   return 0;
  153. }
  154.  
  155. /* Save current data space in the file, update header.  */
  156.  
  157. save_data_space(file, hdr, auxhdr, size)
  158.      int file;
  159.      struct header *hdr;
  160.      struct som_exec_auxhdr *auxhdr;
  161.      int size;
  162. {
  163.   /* Write the entire data space out to the file */
  164.   if (write(file, (void *)auxhdr->exec_dmem, size) != size)
  165.     { perror("Can't save new data space"); exit(1); }
  166.   
  167.   /* Update the header to reflect the new data size */
  168.   auxhdr->exec_dsize = size;
  169.   auxhdr->exec_bsize = 0;
  170. }
  171.  
  172. /* Update the values of file pointers when something is inserted.  */
  173.  
  174. update_file_ptrs(file, hdr, auxhdr, location, offset)
  175.      int file;
  176.      struct header *hdr;
  177.      struct som_exec_auxhdr *auxhdr;
  178.      unsigned int location;
  179.      int offset;
  180. {
  181.   struct subspace_dictionary_record subspace;
  182.   int i;
  183.   
  184.   /* Increase the overall size of the module */
  185.   hdr->som_length += offset;
  186.   
  187.   /* Update the various file pointers in the header */
  188. #define update(ptr) if (ptr > location) ptr = ptr + offset
  189.   update(hdr->aux_header_location);
  190.   update(hdr->space_strings_location);
  191.   update(hdr->init_array_location);
  192.   update(hdr->compiler_location);
  193.   update(hdr->symbol_location);
  194.   update(hdr->fixup_request_location);
  195.   update(hdr->symbol_strings_location);
  196.   update(hdr->unloadable_sp_location);
  197.   update(auxhdr->exec_tfile);
  198.   update(auxhdr->exec_dfile);
  199.   
  200.   /* Do for each subspace dictionary entry */
  201.   lseek(file, hdr->subspace_location, 0);
  202.   for (i = 0; i < hdr->subspace_total; i++)
  203.     {
  204.       if (read(file, &subspace, sizeof(subspace)) != sizeof(subspace))
  205.     { perror("Can't read subspace record"); exit(1); }
  206.       
  207.       /* If subspace has a file location, update it */
  208.       if (subspace.initialization_length > 0 
  209.       && subspace.file_loc_init_value > location)
  210.     {
  211.       subspace.file_loc_init_value += offset;
  212.       lseek(file, -sizeof(subspace), 1);
  213.       if (write(file, &subspace, sizeof(subspace)) != sizeof(subspace))
  214.         { perror("Can't update subspace record"); exit(1); }
  215.     }
  216.     } 
  217.   
  218.   /* Do for each initialization pointer record */
  219.   /* (I don't think it applies to executable files, only relocatables) */
  220. #undef update
  221. }
  222.  
  223. /* Read in the header records from an a.out file.  */
  224.  
  225. read_header(file, hdr, auxhdr)
  226.      int file;
  227.      struct header *hdr;
  228.      struct som_exec_auxhdr *auxhdr;
  229. {
  230.   
  231.   /* Read the header in */
  232.   lseek(file, 0, 0);
  233.   if (read(file, hdr, sizeof(*hdr)) != sizeof(*hdr))
  234.     { perror("Couldn't read header from a.out file"); exit(1); }
  235.   
  236.   if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC
  237.       &&  hdr->a_magic != DEMAND_MAGIC)
  238.     {
  239.       fprintf(stderr, "a.out file doesn't have legal magic number\n"); 
  240.       exit(1);  
  241.     }
  242.   
  243.   lseek(file, hdr->aux_header_location, 0);
  244.   if (read(file, auxhdr, sizeof(*auxhdr)) != sizeof(*auxhdr))
  245.     {
  246.       perror("Couldn't read auxiliary header from a.out file");
  247.       exit(1);
  248.     }  
  249. }
  250.  
  251. /* Write out the header records into an a.out file.  */
  252.  
  253. write_header(file, hdr, auxhdr)
  254.      int file;
  255.      struct header *hdr;
  256.      struct som_exec_auxhdr *auxhdr;
  257. {
  258.   /* Update the checksum */
  259.   hdr->checksum = calculate_checksum(hdr);
  260.   
  261.   /* Write the header back into the a.out file */
  262.   lseek(file, 0, 0);
  263.   if (write(file, hdr, sizeof(*hdr)) != sizeof(*hdr))
  264.     { perror("Couldn't write header to a.out file"); exit(1); }
  265.   lseek(file, hdr->aux_header_location, 0);
  266.   if (write(file, auxhdr, sizeof(*auxhdr)) != sizeof(*auxhdr))
  267.     { perror("Couldn't write auxiliary header to a.out file"); exit(1); }
  268. }
  269.  
  270. /* Calculate the checksum of a SOM header record. */
  271.  
  272. calculate_checksum(hdr)
  273.      struct header *hdr;
  274. {
  275.   int checksum, i, *ptr;
  276.   
  277.   checksum = 0;  ptr = (int *) hdr;
  278.   
  279.   for (i=0; i<sizeof(*hdr)/sizeof(int)-1; i++)
  280.     checksum ^= ptr[i];
  281.   
  282.   return(checksum);
  283. }
  284.  
  285. /* Copy size bytes from the old file to the new one.  */
  286.  
  287. copy_file(old, new, size)
  288.      int new, old;
  289.      int size;
  290. {
  291.   int len;
  292.   int buffer[8192];  /* word aligned will be faster */
  293.   
  294.   for (; size > 0; size -= len)
  295.     {
  296.       len = min(size, sizeof(buffer));
  297.       if (read(old, buffer, len) != len)
  298.     { perror("Read failure on a.out file"); exit(1); }
  299.       if (write(new, buffer, len) != len)
  300.     { perror("Write failure in a.out file"); exit(1); }
  301.     }
  302. }
  303.  
  304. /* Copy the rest of the file, up to EOF.  */
  305.  
  306. copy_rest(old, new)
  307.      int new, old;
  308. {
  309.   int buffer[4096];
  310.   int len;
  311.   
  312.   /* Copy bytes until end of file or error */
  313.   while ( (len = read(old, buffer, sizeof(buffer))) > 0)
  314.     if (write(new, buffer, len) != len) break;
  315.   
  316.   if (len != 0)
  317.     { perror("Unable to copy the rest of the file"); exit(1); }
  318. }
  319.  
  320. #ifdef    DEBUG
  321. display_header(hdr, auxhdr)
  322.      struct header *hdr;
  323.      struct som_exec_auxhdr *auxhdr;
  324. {
  325.   /* Display the header information (debug) */
  326.   printf("\n\nFILE HEADER\n");
  327.   printf("magic number %d \n", hdr->a_magic); 
  328.   printf("text loc %.8x   size %d \n", auxhdr->exec_tmem, auxhdr->exec_tsize);
  329.   printf("data loc %.8x   size %d \n", auxhdr->exec_dmem, auxhdr->exec_dsize);
  330.   printf("entry     %x \n",   auxhdr->exec_entry);
  331.   printf("Bss  segment size %u\n", auxhdr->exec_bsize);
  332.   printf("\n");
  333.   printf("data file loc %d    size %d\n",
  334.      auxhdr->exec_dfile, auxhdr->exec_dsize);
  335.   printf("som_length %d\n", hdr->som_length);
  336.   printf("unloadable sploc %d    size %d\n",
  337.      hdr->unloadable_sp_location, hdr->unloadable_sp_size);
  338. }
  339. #endif /* DEBUG */
  340.  
  341. #ifdef HPUX_USE_SHLIBS
  342. /* Added machinery for shared libs... see comments at the beginning of this file. */
  343.  
  344. void *Brk_On_Dump = 0;        /* Brk value to restore... stored as a global */
  345.  
  346. void Save_Shared_Data () {
  347.   Brk_On_Dump = sbrk( 0 );
  348. }
  349.       
  350. void Restore_Shared_Data () {
  351.   brk ( Brk_On_Dump );
  352. }
  353.  
  354. int run_time_remap (int d) {
  355.   Restore_Shared_Data();
  356. }
  357.  
  358. /* run_time_remap is the magic called by startup code in the dumped executable
  359.  * if RUN_TIME_REMAP is set. 
  360.  */
  361. #endif /* HPUX_USE_SHLIBS */
  362.